<template>
	<div class="system-role-container">
		<el-card shadow="hover">
			<div style="padding: 0 0 10px 0;">
				<el-input
					size="small"
					v-model="query.keywords"
					placeholder="请输入角色名称"
					prefix-icon="el-icon-search"
					style="max-width: 180px"
				></el-input>
				<el-button
					:disabled="!(auth.indexOf('system.role.query') > -1)"
					size="small"
					type="primary"
					class="ml10"
					@click="initTableData"
				>查询</el-button>
				<el-button
					:disabled="!(auth.indexOf('system.role.add') > -1)"
					size="small"
					type="danger"
					class="ml10"
					@click="onOpenAdd"
				>添加</el-button>
			</div>
			<el-table :data="tableData.data" stripe style="width: 100%">
				<el-table-column prop="ID" align="center" label="ID" show-overflow-tooltip></el-table-column>
				<el-table-column prop="RoleName" align="center" label="角色名称" show-overflow-tooltip></el-table-column>
				<el-table-column label="更新时间" show-overflow-tooltip>
					<template #default="scope">{{ timeSpanformat(scope.row.CreatedAt) }}</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="90">
					<template #default="scope">
						<el-button
							size="mini"
							type="text"
							:disabled="!(auth.indexOf('system.role.edit') > -1)"
							@click="onOpenEdit(scope.row)"
						>编辑</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="tableData.param.pageNum"
				background
				v-model:page-size="tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			></el-pagination>
		</el-card>
		<Add @sendRefresh="initTableData" ref="addRoleRef" />
		<Edit @sendRefresh="initTableData" ref="editRoleRef" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref,defineComponent } from 'vue';
import { useStore } from '/@/store/index';
import { timeSpanformat } from '/@/utils/formatTime';
import { getList } from '/@/api/role/index';
import Add from './component/add.vue';
import Edit from './component/edit.vue';
export default defineComponent({
	name: 'Role',
	components: { Add, Edit },
	setup() {
		const store = useStore();
		const auth = store.state.userInfos.userInfos.authPageList;
		const addRoleRef = ref();
		const editRoleRef = ref();
		const state: any = reactive({
			query: {
				keywords: "",
				size: 10,
				page: 0
			},
			isShowDialog: false,
			detailData: [],
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10
				},
			},
		});
		const initTableData = () => {
			getList(state.query).then((res) => {
				if (res.code === 200) {
					state.tableData.data = res.list;
					state.tableData.total = res.list.length;
				}
			})
		};
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
		};
		// 打开新增菜单弹窗
		const onOpenAdd = (row: object) => {
			addRoleRef.value.openDialog(row);
		};
		// 打开编辑菜单弹窗
		const onOpenEdit = (row: object) => {
			editRoleRef.value.openDialog(row);
		};
		const showDetail = (data: any) => {
			state.detailData = data;
			state.isShowDialog = true;
		}
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			addRoleRef,
			editRoleRef,
			onOpenAdd,
			onOpenEdit,
			onHandleSizeChange,
			onHandleCurrentChange,
			...toRefs(state),
			timeSpanformat,
			initTableData,
			auth,
			showDetail
		};
	},
});
</script>
